package com.example.uipractice;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainAct extends Activity {
	private ResponseReceiver receiver;

	public void startService(View view) {
		EditText input = (EditText) findViewById(R.id.txt_input);
		String strInputMsg = input.getText().toString();
		Intent msgIntent = new Intent(this, SimpleIntentService.class);
		msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg);
		if (startService(msgIntent) == null) {
 	       TextView result = (TextView) findViewById(R.id.txt_result);
 	       result.setText("No service!");		
		}
		
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_act);
        
        IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        receiver = new ResponseReceiver();
        registerReceiver(receiver, filter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_act, menu);
        return true;
    }
    
    
    public class ResponseReceiver extends BroadcastReceiver {
    	   public static final String ACTION_RESP =
    	      "com.example.uipractice.MESSAGE_PROCESSED";
    	   
    	   @Override
    	    public void onReceive(Context context, Intent intent) {
    	       TextView result = (TextView) findViewById(R.id.txt_result);
    	       String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG);
    	       result.setText(text);
    	    }
    }
    
}
